package edu.fudan.jwxt.utils;

import cn.hutool.core.lang.Pair;

import java.util.*;

public class TimeUtils {

    public static Map<Integer, String> numberWeekDays = new HashMap<Integer, String>() {{
        put(1, "周一");
        put(2, "周二");
        put(3, "周三");
        put(4, "周四");
        put(5, "周五");
        put(6, "周六");
        put(7, "周日");
    }};

    public static String specificTimeToTime(List<Pair<Integer, Integer>> list){
        if (list.isEmpty()){
            return "暂无课时";
        }else{
            ArrayList<ArrayList<Integer>> arrayLists = new ArrayList<>();
            for (int i=0;i<7;i+=1) arrayLists.add(new ArrayList<>());
            for (Pair<Integer, Integer> pair: list){
                arrayLists.get(pair.getKey()-1).add(pair.getValue());
            }
            for (int i=0;i<7;i+=1){
                if (!arrayLists.get(i).isEmpty()){
                    arrayLists.get(i).sort(Comparator.naturalOrder());
                }
            }
            String ans = new String();
            for (int i=0;i<7;i+=1){
                if (!arrayLists.get(i).isEmpty()){
                    if (!ans.toString().equals("")){
                        ans += ", ";
                    }
                    ans += numberWeekDays.get(i + 1);
                    for (Integer integer: arrayLists.get(i)){
                        ans += " " + integer.toString();
                    }
                }
            }
            return ans;
        }
    }

    public static  List<Pair<Integer,List<Integer>>> specificTimeToArrayTime(List<Pair<Integer, Integer>> list){
        if (list.isEmpty()){
            return null;
        }else {
            List<Pair<Integer,List<Integer>>> arrayLists = new ArrayList<>();
            for (int i = 0; i < 7; i += 1) arrayLists.add(new Pair<>(i+1, new ArrayList<>()));
            for (Pair<Integer, Integer> pair : list) {
                arrayLists.get(pair.getKey() - 1).getValue().add(pair.getValue());
            }
            for (int i = 0; i < 7; i += 1) {
                if (!arrayLists.get(i).getValue().isEmpty()) {
                    arrayLists.get(i).getValue().sort(Comparator.naturalOrder());
                }
            }
            return arrayLists;
        }
    }

    public static List<Pair<Integer, Integer>> exactClassTimeToSpecificTime(List<String> list){
        List<Pair<Integer, Integer>> ans = new ArrayList<>();
        if (list == null){
            return ans;
        }
        for (String string: list){
            if (!Objects.equals(string, "")){
                String[] subString = string.split("-");
                Integer a = Integer.parseInt(subString[0]), b = Integer.parseInt(subString[1]);
                ans.add(new Pair<>(a, b));
            }
        }
        return ans;
    }

    public static String exactClassTimeToExactClassTimeString(List<String> list){
        String ans = "";
        for (int i=0;i<list.size();i+=1){
            if (i==0){
                ans += list.get(i);
            }else{
                ans += "," + list.get(i);
            }
        }
        return ans;
    }

    public static List<String> exactClassTimeStringToExactClassTime(String exactClassTime){
        String[] list = exactClassTime.split(",");
        List<String> list1 = new ArrayList<>();
        for (String string: list){
            list1.add(string);
        }
        return list1;
    }
}
